﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Drawing;

namespace QExplorer
{
	class FavView : ListView
	{
		private QForm form;
		public QExplorer.QForm Form
		{
			get { return form; }
			set { form = value; }
		}
		private DateTime lastRenew;

		public FavView()
		{
			InitializeComponent();
		}
		public FavView(QForm myform)
			: base()
		{
			form = myform;
			InitializeComponent();
			ListViewItemSorter = new FavViewComparer();
		}
		private void InitializeComponent()
		{
			this.SuspendLayout();
			// 
			// FavView
			// 
			this.Activation = System.Windows.Forms.ItemActivation.OneClick;
			this.AllowDrop = true;
			this.Font = new System.Drawing.Font("微软雅黑", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
			this.HoverSelection = true;
			this.LabelEdit = true;
			this.ShowItemToolTips = true;
			this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.FavView_MouseClick);
			this.DragDrop += new System.Windows.Forms.DragEventHandler(this.FavView_DragDrop);
			this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.FavView_MouseMove);
			this.DragEnter += new System.Windows.Forms.DragEventHandler(this.FavView_DragEnter);
			this.DragLeave += new System.EventHandler(this.FavView_DragLeave);
			this.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.FavView_ItemDrag);
			this.DragOver += new System.Windows.Forms.DragEventHandler(this.FavView_DragOver);
			this.ResumeLayout(false);

		}

		private void FavView_DragDrop(object sender, DragEventArgs e)
		{
			string path = (string)e.Data.GetData(typeof(string));
			if (path != null)
			{
				AddItem(path);
			}
		}

		public void AddItem(string path)
		{
			if ((DateTime.Now - lastRenew).TotalMinutes > 1)
			{
				foreach (ListViewItem ite in Items)
				{
					ite.Tag = (int)((int)ite.Tag * 0.75);
				}
				lastRenew = DateTime.Now;
			}
			ListViewItem[] paths = Items.Find(path, false);
			if (paths.Length > 0)
			{
				ListViewItem it = paths[0];
				it.Tag = Convert.ToInt32(it.Tag) + 100;
				it.ToolTipText = path + "\n" + it.Tag.ToString();
				Sort();
				return;
			}
			ListViewItem item = new ListViewItem();
			item.Name = path;
			SHFILEINFO sfi = new SHFILEINFO();
			API.GetFileInfo(path, ref sfi, API.SHGFI_DISPLAYNAME);
			item.Text = sfi.szDisplayName;
			API.GetFileInfo(path, ref sfi, API.SHGFI_SYSICONINDEX);
			item.ImageIndex = sfi.iIcon;
			item.Tag = 100;
			item.ToolTipText = path + "\n" + item.Tag.ToString();
			Items.Add(item);

			while (Items.Count > 10)
			{
				Items.RemoveAt(10);
			}
		}

		private void FavView_DragEnter(object sender, DragEventArgs e)
		{
			e.Effect = DragDropEffects.Link;
		}

		private void FavView_MouseClick(object sender, MouseEventArgs e)
		{
			switch (e.Button)
			{
			case MouseButtons.Left:
				if (SelectedItems.Count == 0)
				{
					return;
				}
				form.Open(SelectedItems[0].Name);
				break;
			case MouseButtons.Right:
				break;

			}
		}
		private void FavView_MouseMove(object sender, MouseEventArgs e)
		{
			if (HitTest(new Point(e.X, e.Y)).Item != null)
			{
				if (Cursor != Cursors.Hand)
				{
					Cursor = Cursors.Hand;
				}
			}
			else
			{
				if (Cursor != Cursors.Default)
				{
					Cursor = Cursors.Default;
				}
			}
		}

		private void FavView_ItemDrag(object sender, ItemDragEventArgs e)
		{
			ListViewItem item = (ListViewItem)e.Item;
			DoDragDrop(item.Name, DragDropEffects.Link);
		}

		private void FavView_DragOver(object sender, DragEventArgs e)
		{
			string path = (string)e.Data.GetData(typeof(string));
			Items.RemoveByKey(path);
		}

		private void FavView_DragLeave(object sender, EventArgs e)
		{
		}

		public void RenewWeights()
		{
		}

		public class FavViewComparer : System.Collections.IComparer
		{
			public int Compare(Object x, Object y)
			{
				ListViewItem a = (ListViewItem)x;
				ListViewItem b = (ListViewItem)y;
				Debug.WriteLine("sort: ");
				return (int)a.Tag - (int)b.Tag;
			}
		}
	}

}
